/**
 * @param {Egg.Application} app - egg application
 */
// 路由规则：
// router.verb('path-match', app.controller.action);
// router.verb('router-name', 'path-match', app.controller.action);
// router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
// router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);
// 说明：
// verb可选：head, options, get, put, post, patch, delete, del, redirect

module.exports = app => {
  const { router, controller, middleware, config } = app;
  const { addParams } = middleware;
  const jwt = middleware.isAuth(config.jwt.secret);
  // const tableName = middleware.inferTableName(app);
  const { common, tool, user, note, topic, notice } = controller;

  /*** 示例相关 ***/
  // router.get("/example/:id", "test.index");
  // router.get("/example/:id", "test.index");
  // router.get("/^\/package\/([\w-.]+\/[\w-.]+)$/,", "test.index");

  /*** 测试相关 ***/
  router.get("/easy-note/test", "test.index");
  router.get("/easy-note/test/user", user.detail);
  router.get("/easy-note/test/notice", notice.unread);
  router.get("/easy-note/test/search", "test.search");
  router.get("/easy-note/test/sqlTest", "test.sqlTest");

  /*** 基础相关 ***/
  // router.get("/", "test.index");
  router.redirect("/", "/easy-note/test", 302); // 重定向到测试页面
  router.get("/easy-note/tool/captcha", tool.getCaptcha);

  /*** 用户相关 ***/
  router.post("/easy-note/user/login", addParams("user"), user.login); // 用户登录
  router.post("/easy-note/user/logout", jwt, addParams("user"), user.logout); // 用户登出
  router.get("/easy-note/user/collect/tree", jwt, addParams("user"), user.collectTree); // 用户收藏夹

  router.post("/easy-note/user/add", addParams("user"), common.insert);
  router.delete("/easy-note/user/delete", jwt, addParams("user"), common.delete);
  router.put("/easy-note/user/update", jwt, addParams("user"), common.update);
  router.get("/easy-note/user/list", jwt, addParams("user", { keyword: ["id", "nickname", "user_name"] }), common.list);
  router.get("/easy-note/user/detail", jwt, addParams("user"), user.detail);

  /*** 笔记相关 ***/
  router.post("/easy-note/note/add", jwt, addParams("note"), common.insert);
  router.delete("/easy-note/note/delete", jwt, addParams("note"), common.delete);
  router.put("/easy-note/note/update", jwt, addParams("note"), common.update);
  router.get("/easy-note/note/list", jwt, addParams("note", { keyword: ["title"], order: ["score", "update_time", "create_time"] }), note.list);
  router.get("/easy-note/note/detail", jwt, addParams("note"), note.detail);

  router.put("/easy-note/note/count/update", jwt, addParams("note"), common.countUpdate);
  router.put("/easy-note/note/operate/toggle", jwt, addParams("note"), common.operateToggle);

  /*** 题目相关 ***/
  router.post("/easy-note/topic/add", jwt, addParams("topic"), common.insert);
  router.delete("/easy-note/topic/delete", jwt, addParams("topic"), common.delete);
  router.put("/easy-note/topic/update", jwt, addParams("topic"), common.update);
  router.get(
    "/easy-note/topic/list",
    jwt,
    addParams("topic", { keyword: ["title", "content"], order: ["score", "update_time", "create_time"] }),
    common.list
  );
  router.get("/easy-note/topic/detail", jwt, addParams("topic"), common.detail);

  /*** 订单相关 ***/
  router.post("/easy-note/orders/add", jwt, addParams("orders"), common.insert);
  router.delete("/easy-note/orders/delete", jwt, addParams("orders"), common.delete);
  router.put("/easy-note/orders/update", jwt, addParams("orders"), common.update);
  router.get("/easy-note/orders/list", jwt, addParams("orders"), common.list);
  router.get("/easy-note/orders/detail", jwt, addParams("orders"), common.detail);

  /*** 意见反馈 ***/
  router.post("/easy-note/feedback/add", jwt, addParams("feedback"), common.insert);
  router.delete("/easy-note/feedback/delete", jwt, addParams("feedback"), common.delete);
  router.put("/easy-note/feedback/update", jwt, addParams("feedback"), common.update);
  router.get("/easy-note/feedback/list", jwt, addParams("feedback"), common.list);
  router.get("/easy-note/feedback/detail", jwt, addParams("feedback"), common.detail);

  /*** 用户举报 ***/
  router.post("/easy-note/report/add", jwt, addParams("report"), common.insert);
  router.delete("/easy-note/report/delete", jwt, addParams("report"), common.delete);
  router.put("/easy-note/report/update", jwt, addParams("report"), common.update);
  router.get("/easy-note/report/list", jwt, addParams("report"), common.list);
  router.get("/easy-note/report/detail", jwt, addParams("report"), common.detail);

  /*** 消息相关 ***/
  router.post("/easy-note/system_notice/add", jwt, addParams("system_notice"), common.insert);
  router.delete("/easy-note/system_notice/delete", jwt, addParams("system_notice"), common.delete);
  router.put("/easy-note/system_notice/update", jwt, addParams("system_notice"), common.update);
  router.get("/easy-note/system_notice/list", jwt, addParams("system_notice"), common.list);
  router.get("/easy-note/system_notice/detail", jwt, addParams("system_notice"), common.detail);
  router.get("/easy-note/system_notice/unread", jwt, addParams("system_notice"), notice.unread);

  /*** 行业相关 ***/
  router.post("/easy-note/industry/add", jwt, addParams("industry"), common.insert);
  router.delete("/easy-note/industry/delete", jwt, addParams("industry"), common.delete);
  router.put("/easy-note/industry/update", jwt, addParams("industry"), common.update);
  router.get("/easy-note/industry/list", jwt, addParams("industry"), common.list);
  router.get("/easy-note/industry/detail", jwt, addParams("industry"), common.detail);

  /*** 字典相关 ***/
  router.resources("dictionary", "/easy-note/dictionary", addParams("dictionary"), controller.dictionary);
};
